#include "ComplexChromosome.h"
template<class T>
ComplexChromosome<T>::ComplexChromosome() : Chromosome()
{

}

template<class T>
ComplexChromosome<T>::ComplexChromosome(int size) : Chromosome(),
                    minValue(0),
                    maxValue(0)
{
    for(int i=0;i<size;++i)
        geneArray.append(0);
}

template<class T>
ComplexChromosome<T>::ComplexChromosome(int size, T min, T max) : Chromosome(),
                    minValue(min),
                    maxValue(max)
{
    for(int i=0;i<size;++i)
        geneArray.append(0);
}

template<class T>
ComplexChromosome<T>::ComplexChromosome(T min, T max) : Chromosome(),
                    minValue(min),
                    maxValue(max)
{
}

template<class T>
ComplexChromosome<T>::ComplexChromosome(const ComplexChromosome &other) : Chromosome(other)
{
    minValue = other.minValue;
    maxValue = other.maxValue;

    geneArray = QList<T>(other.geneArray);
}

template<class T>
ComplexChromosome<T>::~ComplexChromosome()
{
}

template<class T>
void ComplexChromosome<T>::setRandom()
{
    for(int i=0;i<geneArray.size();++i)
    {
        geneArray.replace(i, getRandomValue());
    }
}

template<class T>
void ComplexChromosome<T>::crossOver(const Chromosome& partner)
{
    //qDebug() << "ComplexChromosome::crossOver";
    if(this->size() != partner.size())
        qFatal("Parents chromosomes lenght is different!");

    int crossOverPosition;
    //cross point randomed, greater than 0, smaller than chromosome size
    crossOverPosition = (qrand() % (geneArray.size() -1)) + 1;
    //qDebug() << "mother: " << *this;
    //qDebug() << "father: " << partner;
    //qDebug() << "crossOverPosition: " << crossOverPosition;

    for(int i=crossOverPosition;i<geneArray.size();++i)
        geneArray.replace(i, ((ComplexChromosome<T>)partner).geneArray.at(i));

    this->setFitness(0);

    //qDebug() << "child:  " << *this;
}

template<class T>
void ComplexChromosome<T>::mutate(int index)
{
    //qDebug() << "mutationPosition: " << index + 1;
    //qDebug() << "before mutation: " << *this;
    geneArray.replace(index, getRandomValue());
    //qDebug() << "after  mutation: " << *this;
}

template<class T>
bool ComplexChromosome<T>::operator==(const Chromosome& other) const
{
    //TODO
    //qDebug() << "ComplexChromosome::operator==";
    return (geneArray == static_cast<const ComplexChromosome&>(other).geneArray);
}

template<class T>
QVariant ComplexChromosome<T>::at(int index) const
{
    return geneArray.at(index);
}

template<class T>
QVariant ComplexChromosome<T>::data() const
{
    QList<QVariant> tmpList;
    for(int i=0;i<geneArray.size();++i)
        tmpList.append(geneArray.at(i));
    return tmpList;
}

template<class T>
int ComplexChromosome<T>::size() const
{
    return geneArray.size();
}
